package org.newbee;

import org.hibernate.engine.jdbc.connections.spi.AbstractMultiTenantConnectionProvider;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;

import java.sql.Connection;
import java.sql.SQLException;


/**
 * Created by yujie on 2017/6/24.
 */
public class SchemaBasedMultiTenantConnectionProvider extends AbstractMultiTenantConnectionProvider {

    private CustomerProvider testProvider = new CustomerProvider("jdbc:mysql://localhost:3306/test", "root", "123456");

    private CustomerProvider test2Provider = new CustomerProvider("jdbc:mysql://localhost:3306/test2", "root", "123456");


    public SchemaBasedMultiTenantConnectionProvider() {
        super();
    }

    @Override
    public Connection getAnyConnection() throws SQLException {
        return super.getAnyConnection();
    }

    @Override
    public void releaseAnyConnection(Connection connection) throws SQLException {
        super.releaseAnyConnection(connection);
    }

    @Override
    public Connection getConnection(String tenantIdentifier) throws SQLException {
        return super.getConnection(tenantIdentifier);
    }

    @Override
    public void releaseConnection(String tenantIdentifier, Connection connection) throws SQLException {
        super.releaseConnection(tenantIdentifier, connection);
    }

    @Override
    public boolean supportsAggressiveRelease() {
        return super.supportsAggressiveRelease();
    }

    @Override
    public boolean isUnwrappableAs(Class unwrapType) {
        return super.isUnwrappableAs(unwrapType);
    }

    @Override
    public <T> T unwrap(Class<T> unwrapType) {
        return super.unwrap(unwrapType);
    }

    @Override
    protected ConnectionProvider getAnyConnectionProvider() {
        return testProvider;
    }

    @Override
    protected ConnectionProvider selectConnectionProvider(String s) {
        if ("test".equalsIgnoreCase(s)) {
            return testProvider;
        }
        return test2Provider;
    }
}
